<?php

class DBTls
{
    function __construct()
    {
        $this->_link = mysql_connect('localhost','root','');
        mysql_select_db('test');
    }    
    function __destruct()
    {
        mysql_close($this->_link);
    }
    private static $_obj = null;
    static function getInstance()
    {
        if( !self::$_obj )
        {
            self::$_obj = new self();
        }

        return self::$_obj;
    }
    
    function query( $sql )
    {
        return $this->update( $sql );
    }
    
    
    function update( $sql )
    {
        $retinfo = array('err'=>'', 'ret'=>'');
        if( !($rs = mysql_query( $sql, $this->_link )) )
        {
            $retinfo['err'] = "mysql_query faild, error=[" . mysql_error() . "]";
            $retinfo['sql'] = $sql;
            $retinfo['ret'] = false;
            return $retinfo;
        }

        $num = mysql_affected_rows();
        $retinfo['num'] = $num;
        $retinfo['ret'] = true;
        return $retinfo;
    }
    
    function insert( $sql )
    {
        return $this->update( $sql );
    }
    
    function select( $sql, &$results )
    {
        $retinfo = array('err'=>'', 'ret'=>'');
        $rs = mysql_query( $sql, $this->_link );
        if( $rs === false )
        {
            $retinfo['err'] = "mysql_query faild, error=[" . mysql_error() . "]";
            $retinfo['sql'] = $sql;
            $retinfo['ret'] = false;
            return $retinfo;
        }

        while ($row=mysql_fetch_array($rs))
        {
            $results[] = $row;
        }
        mysql_free_result($rs); 

        $retinfo['ret'] = true;
        return $retinfo;
    }
}

























